<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { usetikuUserStore } from '@/stores/sj-user'
import { ElMessage } from 'element-plus'
import { getAccessibleMenus } from '@/utils/permission'

const router = useRouter()
const userStore = usetikuUserStore()

// 获取用户信息
const userInfo = computed(() => userStore.userInfo)

// 控制侧边栏折叠状态
const isCollapse = ref(false)

// 当前激活的菜单项
const activeMenu = ref('dashboard')

// 根据用户角色获取菜单
const menus = computed(() => {
  return getAccessibleMenus(userInfo.value)
})

// 处理菜单点击
const handleMenuSelect = (index) => {
  activeMenu.value = index
  // 根据菜单项导航到对应路由
  router.push(`/home/${index}`)
}

// 处理退出登录
const handleLogout = () => {
  userStore.logout()
  ElMessage.success('退出登录成功')
  router.push('/sj-login')
}

// 切换侧边栏折叠状态
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value
}
</script>

<template>
  <div class="layout-container">
    <el-container class="layout-container-main">
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '220px'" class="sidebar-container">
        <div class="logo-container">
          <span v-if="!isCollapse">题库管理系统</span>
          <el-icon v-else><Files /></el-icon>
        </div>
        
        <el-menu
          :default-active="activeMenu"
          class="sidebar-menu"
          :collapse="isCollapse"
          @select="handleMenuSelect"
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
        >
          <el-menu-item v-for="item in menus" :key="item.index" :index="item.index">
            <el-icon><component :is="item.icon" /></el-icon>
            <template #title>{{ item.title }}</template>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <el-container class="main-container">
        <!-- 顶部导航栏 -->
        <el-header class="header-container">
          <div class="header-left">
            <el-icon class="toggle-button" @click="toggleSidebar">
              <Fold v-if="!isCollapse" />
              <Expand v-else />
            </el-icon>
          </div>
          
          <div class="header-right">
            <el-dropdown trigger="click">
              <div class="user-info">
                <el-avatar :size="32" icon="UserFilled" />
                <span class="username">{{ userInfo?.account || '用户' }}</span>
                <el-icon><ArrowDown /></el-icon>
              </div>
              
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="router.push('/home/profile')">
                    <el-icon><User /></el-icon>个人中心
                  </el-dropdown-item>
                  <el-dropdown-item divided @click="handleLogout">
                    <el-icon><SwitchButton /></el-icon>退出登录
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>
        
        <!-- 主内容区 -->
        <el-main class="main-content">
          <div class="content-container">
            <!-- 面包屑导航 -->
            <el-breadcrumb separator="/" class="breadcrumb">
              <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item>{{ menus.find(item => item.index === activeMenu)?.title }}</el-breadcrumb-item>
            </el-breadcrumb>
            
            <!-- 内容区域 -->
            <div class="page-container">
              <router-view />
              
              <!-- 欢迎页面，只在没有子路由时显示 -->
              <el-card v-if="$route.path === '/home'">
                <template #header>
                  <div class="card-header">
                    <span>欢迎使用题库管理系统</span>
                  </div>
                </template>
                <div class="card-content">
                  <h3>{{ userInfo?.account || '用户' }}，您好！</h3>
                  <p>您已成功登录系统，请使用左侧菜单访问各功能模块。</p>
                  <p>当前版本：v6.1.0</p>
                </div>
              </el-card>
            </div>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<style scoped>
.layout-container {
  height: 100vh;
  width: 100%;
}

.layout-container-main {
  height: 100%;
}

/* 侧边栏样式 */
.sidebar-container {
  background-color: #304156;
  transition: width 0.3s;
  overflow: hidden;
}

.logo-container {
  height: 60px;
  line-height: 60px;
  background-color: #263445;
  text-align: center;
  color: #fff;
  font-size: 18px;
  font-weight: bold;
  overflow: hidden;
}

.sidebar-menu {
  border-right: none;
  height: calc(100% - 60px);
}

/* 顶部导航栏样式 */
.header-container {
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 15px;
}

.header-left {
  display: flex;
  align-items: center;
}

.toggle-button {
  font-size: 20px;
  cursor: pointer;
  color: #606266;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin: 0 5px;
  color: #606266;
}

/* 主内容区样式 */
.main-container {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.main-content {
  background-color: #f0f2f5;
  padding: 15px;
  height: calc(100% - 60px);
  overflow-y: auto;
}

.content-container {
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  padding: 20px;
  min-height: calc(100% - 40px);
}

.breadcrumb {
  margin-bottom: 15px;
  padding: 8px 0;
}

.page-container {
  min-height: 300px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-content {
  text-align: center;
  padding: 20px;
}
</style>